package com.ocom.payhandle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfDeviceInfo;
import com.ocom.common.entity.srvpaccess.XfWhiteInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.vo.pyhandle.GetComWhiteListVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payhandle.dao.IDaoXfWhiteInfo;
import com.ocom.payhandle.service.XfWhiteInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class XfWhiteInfoServiceImpl extends ServiceImpl<IDaoXfWhiteInfo, XfWhiteInfo> implements XfWhiteInfoService {


    @Autowired
    private IDaoXfWhiteInfo xfWhiteInfo;

    /*
     * @Description:查询最大的id
     * @Author: lu
     * @Date: 2022/6/27 15:50
     * @param companyId:
     * @return: com.ocom.common.entity.srvpaccess.XfWhiteInfo
     **/
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public XfWhiteInfo selectMaxId(Long companyId) {
        QueryWrapper<XfWhiteInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("max(id) as id");
        queryWrapper.eq("com_id",companyId);
        return xfWhiteInfo.selectOne(queryWrapper);
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public boolean deleteXfWhite(Long companyId, Integer pId) {
        QueryWrapper<XfWhiteInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("p_id",pId);
        return this.remove(queryWrapper);
    }

    /*
     * @Description:根据companyId查询白名单数
     * @Author: lu
     * @Date: 2022/6/27 15:50
     * @param companyId:
     * @param listFormat:
     * @return: java.util.List<com.ocom.common.entity.srvpaccess.XfWhiteInfo>
     **/
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<XfWhiteInfo> selectList(Long companyId, int listFormat, Integer pId, Integer listState) {
        QueryWrapper<XfWhiteInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",companyId);
        if (listState!=null){
            if (listState==0){
                queryWrapper.gt("id",pId);
                queryWrapper.eq("state",0);
            }else {
                queryWrapper.gt("id",pId);
                queryWrapper.eq("state",1);
            }
        }else {
            queryWrapper.eq("state",0);
        }
        queryWrapper.orderByAsc("id");
        queryWrapper.last("limit "+listFormat);
        return xfWhiteInfo.selectList(queryWrapper);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public XfWhiteInfo selectXFWhite(Long companyId, EntityXfDeviceInfo xfDeviceInfo) {
        QueryWrapper<XfWhiteInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",companyId);
        queryWrapper.and(wrapper -> {
            wrapper.and(itemWrapper ->{
                itemWrapper.gt("id",xfDeviceInfo.getListAddTab());
                itemWrapper.eq("state",0);
            });
            wrapper.or(itemWrapper -> {
                itemWrapper.gt("id", xfDeviceInfo.getListDeleteTab());
                itemWrapper.eq("state", 1);
            });
        });
        queryWrapper.orderByAsc("id");
        queryWrapper.last("limit 1");
        return xfWhiteInfo.selectOne(queryWrapper);
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public List<GetComWhiteListVo> getComWhiteList(Long companyId, Integer personVer, Integer listLimit) {
        return xfWhiteInfo.getComWhiteList(companyId, personVer, listLimit);
    }
}
